package com.yxzx.oss.controller;

import com.yxzx.common.YxzxResult;
import com.yxzx.oss.service.FileService;
import com.yxzx.oss.util.ConstantPropertiesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@Api("阿里云文件管理")
@CrossOrigin
@RestController
@RequestMapping("/ossservice/file")
public class FileUploadController {
    @Autowired
    private FileService fileService;

    @ApiOperation("文件上传")
    @PostMapping("upload")
    public YxzxResult upload(@ApiParam(name="file",value="文件",required = true)
                                         @RequestParam("file") MultipartFile file,
                             @ApiParam(name = "host",value = "文件上传路径",required = false)
                             @RequestParam(value = "host",required = false) String host){
        log.info("文件上传host参数======"+host);
        if (!StringUtils.isEmpty(host)){
            ConstantPropertiesUtil.FILE_HOST = host;
        }
        String uploadUrl = fileService.upload(file);
        return YxzxResult.ok().message("文件上传成功").data("url",uploadUrl);
    }
}
